//
// Created by ct on 2020/10/27.
//

#ifndef NETTOP_ASYNC_LOG_H
#define NETTOP_ASYNC_LOG_H
#include <chrono>
#include <string>
#include <memory>
#include <thread>
#include "name_res.h"
#include "mt_list.h"
namespace nettop {
    class async_line{
        const std::chrono::system_clock::time_point tp_;
    public:
        async_line(const  async_line&) = delete;
        async_line& operator=(const async_line&) = delete;

        async_line() : tp_(std::chrono::system_clock::now()) {
        }
        const std::chrono::system_clock::time_point& get_tp(void) {
            return tp_;
        }

        virtual std::string log(name_res& nr) const = 0;

        virtual ~async_line() = default;;
    };

    typedef std::shared_ptr<async_line> sp_async_line;
    typedef mt_list<sp_async_line>		async_log_list;

    class async_log {
        async_log(const async_log&) = delete;
        async_log& operator=(const async_log&) = delete;

        volatile bool&			exit_;
        name_res&			nr_;
        std::shared_ptr<std::ostream>	ostr_;
        async_log_list&			list_;
        std::shared_ptr<std::thread>	thrd_;

        void thread_proc(void);
    public:
        async_log(volatile bool& e, name_res& nr, const std::string& fname, async_log_list& list);

        ~async_log();
    };
}

#endif //NETTOP_ASYNC_LOG_H
